/***
 * 主框架JS
 * ***/
 import {reactive} from "vue";
 import router from "@/router.js";
 
 
 //默认数据
 export const frameworkData=reactive({
     isMenu:false,//处理内容屏幕宽度
     isFullScreen:false,//是否显示全屏按钮
     defaultPath:localStorage.getItem("defaultPath") ? localStorage.getItem("defaultPath") : import.meta.env.VITE_APP_SERVER_NAME, //默认服务地址
     tag:JSON.parse(localStorage.getItem("tag")) ? JSON.parse(localStorage.getItem("tag")) : [{name:"首页",path:"/"}] 
 })
 
 
 export const onFullScreen=()=>{  //打开全屏
     frameworkData.isFullScreen = true;
     let element = document.documentElement;
     if (element.requestFullscreen) {
       element.requestFullscreen();
     } else if (element.msRequestFullscreen) {
       element.msRequestFullscreen();
     } else if (element.mozRequestFullScreen) {
       element.mozRequestFullScreen();
     } else if (element.webkitRequestFullscreen) {
       element.webkitRequestFullscreen();
     }
 };
 
 
 export const onExitScreen=()=>{ //退出全屏
     frameworkData.isFullScreen = false;
     if (document.exitFullscreen) {
       document.exitFullscreen();
     } else if (document.msExitFullscreen) {
       document.msExitFullscreen();
     } else if (document.mozCancelFullScreen) {
       document.mozCancelFullScreen();
     } else if (document.webkitExitFullscreen) {
       document.webkitExitFullscreen();
     }
   };
 
 
 export const  onMenu=()=>{ //导航显示
     frameworkData.isMenu= frameworkData.isMenu ?false:true;
 }
 
 
  /**
  * 数组去重
  * @param {*} arr 接收的原数组
  * @param {*} key 如果是对象数组[{id: 1}, {id: 2}, {id: 3}]，则需要以什么key作为重复的标准，普通数组[1,2,3,2]不需要
  */
   export const arrUnique=(arr, key)=>{//数组去重
     let returnArr = [];
     if (key) {
         // 对象数组去重
         const obj = {};
         returnArr = arr.reduce((cur, next) => {
             obj[next[key]] ? '' : obj[next[key]] = true && cur.push(next);
             return cur;
         }, []);
         return returnArr;
     }
     // 普通数组去重
     returnArr = arr.reduce((cur, next) => {
         !cur.includes(next) && cur.push(next);
         return cur;
     }, []);
     return returnArr;
 }
 
 
  
  export const goto=(itme)=>{//服务跳转处理
     console.log(itme,'============================================')
     location.href=itme.link;
     frameworkData.defaultPath=itme.serviceName;//默认选中
     localStorage.setItem("defaultPath",frameworkData.defaultPath);
     localStorage.setItem("tag",JSON.stringify(frameworkData.tag));
  }  
 
  export const gotoNav=(itme,defaultPath)=>{//导航跳转处理
       router.push({path:itme.router});//跳转
       frameworkData.defaultPath=defaultPath;
       localStorage.setItem("defaultPath",defaultPath);
       frameworkData.tag.push({name:itme.name,path:itme.router,defaultPath:defaultPath});
       frameworkData.tag=arrUnique(frameworkData.tag,'path');//JS去重
       console.log("======导航跳转处理=========>",frameworkData.tag)
       localStorage.setItem("tag",JSON.stringify(frameworkData.tag));
 } 
 
 export const onTagUrl=(itme)=>{ //tag跳转地址
       console.log("tag跳转地址===>",itme)
       frameworkData.defaultPath=itme.serviceName;
       localStorage.setItem("defaultPath",itme.serviceName);
       if(itme.path){
         router.push({path:itme.path});//跳转
       }
       }
      
 
 export const onTagDel=(itme)=>{//删除当前标签
    frameworkData.tag.splice(frameworkData.tag.indexOf(itme),1);//删除
    if(frameworkData.tag.length>0) {
      let item=frameworkData.tag.slice(-1);//获取最后一个数组
      localStorage.setItem("tag",JSON.stringify(frameworkData.tag));
      router.push({path:item[0].path});//跳转地址
    }else{
      router.push({path:"/"});//跳转首页
    }
 }